/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package portal.system;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Riso-Desktop
 */
@WebServlet(name = "VypisMenu", urlPatterns = {"/menu"})
public class VypisMenu extends HttpServlet {

    PrintWriter out;
    String driver = portal.constants.Database.DATABASE_DRIVER;
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    String userName = portal.constants.Database.DATABASE_USER;
    String password = portal.constants.Database.DATABASE_PASSWORD;
    String URL = portal.constants.Database.DATABASE_URL;
    HttpSession session;

    @Override
    public void init() {
        try {
            super.init();
            Class.forName(driver);
            con = DriverManager.getConnection(URL, userName, password);
        } catch (Exception ex) {
            System.err.println("Nepodarilo sa pripojiť k databáze");
            ex.printStackTrace();
        }
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            vypisMenu(request, response);
        } finally {
            //out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private synchronized void vypisMenu(HttpServletRequest request, HttpServletResponse response) {

        if (request.getParameter("kategoria") != null||request.getParameter("podkategoria") != null) {
            vypisPodkategorie(request, response);
        } else {
            vypisKategorie(request, response);
        }

    }

    private void vypisPodkategorie(HttpServletRequest request, HttpServletResponse response) {
        try {

            stmt = con.createStatement();
            String sql="";
            if(request.getParameter("kategoria")!=null){
                sql = "select * from podkategorie where id_kategorie = " + request.getParameter("kategoria") + " order by nazov_podkategorie asc";
            }else { 
                sql = "SELECT * FROM podkategorie WHERE id_kategorie = (select max(id_kategorie) from podkategorie where id_podkategorie = "+request.getParameter("podkategoria") +") order by nazov_podkategorie asc";
            }
            rs = stmt.executeQuery(sql);
            //vypisCSSzac();
            int pocet = 0;
            ArrayList obrazky = new ArrayList();
            while (rs.next()) {
                pocet++;
                obrazky.add(rs.getString("id_podkategorie"));
            }
            rs.beforeFirst();
            //vypisCSSkon(pocet, obrazky,"pod");
            out.println("<div align=\"center\">");
            out.println("<section class=\"slide-up-boxes\">");
            while (rs.next()) {
                out.print("<a href=\"./inzeraty.jsp?podkategoria=" + rs.getInt("id_podkategorie") + "\"  class=\"pod" + rs.getInt("id_podkategorie") + "\">");
                out.print("<h5>" + rs.getString("nazov_podkategorie") + "</h5>" + "<div>" + rs.getString("nazov_podkategorie") + "</div>");
                out.println("</a>");
                out.println("<br/>");
            }
            out.println("<a href=\"./index.jsp\"  class=\"spet\"><h5>Späť</h5><div>Späť</div></a>");
            out.println("</section>");
            out.println("</div>");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private void vypisKategorie(HttpServletRequest request, HttpServletResponse response) {
        try {
            stmt = con.createStatement();
            String sql = "select nazov_kategorie, id_kategorie from kategorie" + " order by nazov_kategorie asc";
            rs = stmt.executeQuery(sql);
            //vypisCSSzac();
            int pocet = 0;
            ArrayList obrazky = new ArrayList();
            while (rs.next()) {
                pocet++;
                obrazky.add(rs.getString("id_kategorie"));
            }
            rs.beforeFirst();
            //vypisCSSkon(pocet, obrazky,"kat");
            out.println("<div align=\"center\">");
            out.println("<section class=\"slide-up-boxes\">");
            while (rs.next()) {
                out.print("<a href=\"./menu.jsp?kategoria=" + rs.getInt("id_kategorie") + "\"  class=\"kat" + rs.getInt("id_kategorie") + "\">");
                out.print("<h5>" + rs.getString("nazov_kategorie") + "</h5>" + "<div>" + rs.getString("nazov_kategorie") + "</div>");
                out.println("</a>");
                out.println("<br/>");
            }
            out.println("</section>");
            out.println("</div>");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private void vypisCSSzac() {
        out.println("<style>"
                + ".slide-up-boxes a { "
                + "display: block; "
                + "background: #eee;"
                + "border: 1px solid #ccc;"
                + "height: 30px; "
                + "width: 180px;"
                + "border-radius: 4px;"
                + "margin: 0 0 -15px 0;"
                + "overflow: hidden; "
                + "text-shadow: 1px 1px #ccc;"
                + "}"
                + ".slide-up-boxes h5 { "
                + "color: #333; "
                + "text-align: center;"
                + "height: 30px; "
                + "line-height: 30px;"
                + "font: italic 12px/30px Georgia, Serif;"
                + " -webkit-transition: all 0.2s linear; "
                + " -moz-transition: all 0.2s linear; "
                + " -o-transition: all 0.2s linear;"
                + "}"
                + ".slide-up-boxes a:hover h5 { "
                + "margin-top: -30px; "
                + "}"
                + ".slide-up-boxes div { "
                + "color: black; "
                + "font: italic 12px/30px Georgia, Serif;"
                + "height: 30px; "
                + "text-align: center;"
                + "opacity: 0; "
                + "-webkit-transform: rotate(6deg); "
                + "-webkit-transition: all 0.4s linear; "
                + "-moz-transform: rotate(6deg); "
                + "-moz-transition: all 0.4s linear; "
                + "-o-transform: rotate(6deg); "
                + "-o-transition: all 0.4s linear; "
                + "}"
                + ".slide-up-boxes a:hover div { "
                + "opacity: 1;"
                + "-webkit-transform: rotate(0); "
                + "-moz-transform: rotate(0); "
                + "-o-transform: rotate(0); "
                + "}");
    }

    private void vypisCSSkon(int pocet, ArrayList idckaobrazkov, String type) {
        if (type.equals("kat")) {
            for (int i = 1; i <= pocet; i++) {
                out.println(".kat" + idckaobrazkov.get(i - 1) + " div { background: #d6ecec url(./displayImage?kat=" + idckaobrazkov.get(i - 1) + ") no-repeat; padding-left: 30px;}");
                //out.println(".slide-up-boxes a:nth-child("+i+") div { background: #d6ecec url(./displayImage?kat="+idckaobrazkov.get(i-1) +") 0px 0px no-repeat; padding-left: 60px;}");
            }
        } else {
            for (int i = 1; i <= pocet; i++) {
                out.println(".pod" + idckaobrazkov.get(i - 1) + " div { background: #d6ecec url(./displayImage?pod=" + idckaobrazkov.get(i - 1) + ") no-repeat; padding-left: 30px;}");
                //out.println(".slide-up-boxes a:nth-child("+i+") div { background: #d6ecec url(./displayImage?kat="+idckaobrazkov.get(i-1) +") 0px 0px no-repeat; padding-left: 60px;}");
            }
        }
        out.println(".spet div { background: #d6ecec url(img/back.png) no-repeat; padding-left: 0px;}");
        out.println("</style>");
    }
}